/**
 * author: zhou
 * date: 2017.10.20
 * description: 科目管理中心
 * functions: {
 *      save: 保存科目信息
 *      findAll: 获取全部科目
 *      findOne: 获取科目信息
 *      update: 更新科目
 * }
 * 
 */

const   mongoPromise = require('../../../core/mongo/mongoPromise'),
        Subject      = require('../../../core/mongo/models/school/subjectModel');

const subjectController = {
    /**
    * 保存科目信息
    * 需要管理学校id
    * @param {*} ctx 
    */
    async save(ctx) {
        const { name, schoolId } = ctx.request.body.data;
        if (!name) {
            ctx.body = { success: false, message: '学科名称不能为空', state: 10004 };
            return;
        }
        if (!schoolId) {
            ctx.body = { success: false, message: '学科id不能为空', state: 10004 };
            return;
        }
        const subject = new Subject({
            name,
            schoolId
        });
        await Subject.create(subject).then((doc) => {
            ctx.body = { success: true, data: [], state: 200 };
        },
        (error) => {
            ctx.body = { success: false, message: '抱歉,保存科目失败', state: 10003 };
        });
    },
    /**
    * 获取科目列表
    * @param {*} ctx 
    */
    async findAll(ctx) {
        const { schoolId, date, limit } = ctx.request.body.data;
        if (!schoolId) {
            ctx.body = { success: false, message: '学校id不能为空', state: 10004 };
            return;
        }
        const queryParam = {
            schoolId
        };
        if (date) {
            queryParam.createDate = { $gt: date };
        }
        let query = Subject.find(queryParam);
        if (limit) {
            query = query.limit(limit);
        }
        const result = await mongoPromise(ctx, query);
        if (result) {
            const countQuery = Subject.count({ schoolId });
            const countResult = await mongoPromise(ctx, countQuery);
            if (countResult !== undefined) {
                ctx.body = { success: true, data: { data: result, count: countResult }, state: 200 };
            }
        } 
    },
    /**
    * 查询科目信息
    * @param {*} ctx 
    */
    async findOne(ctx) {
        const { id } = ctx.request.body.data;
        if (!id) {
            ctx.body = { success: false, message: '科目id不能为空', state: 10004 };
            return;
        }
        const query = Subject.findById(id);
        const result = await mongoPromise(ctx, query);
        if (result !== undefined) {
            ctx.body = { success: true, data: result, state: 200 };
        } else {
            ctx.body = { success: false, message: '抱歉,未查找到该科目', state: 200 };
        }
    },
    /**
    * 更新科目信息
    * @param {*} ctx 
    */
    async update(ctx) {
        const { _id, name } = ctx.request.body.data;
        if (!_id) {
            ctx.body = { success: false, message: '科目id不能为空', state: 10004 };
            return;
        }
        if (!name) {
            ctx.body = { success: false, message: '科目名称不能为空', state: 10004 };
            return;
        }
        const query = Subject.update({ _id }, { $set: { name }});
        const result = await mongoPromise(ctx, query);
        if (result) {
            ctx.body = { success: true, data: [], state: 200 };
        }
    }
};

module.exports = subjectController;